package com.winit.openapi.apiadapter.adapter;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.mms.spi.command.ItemForCustomerCommand;
import com.winit.mms.spi.command.ModifyProductForCustomerCommand;
import com.winit.mms.spi.v2.item.ItemService;
import com.winit.mms.spi.v2.item.command.ModifyItemForCustomerCommand;
import com.winit.mms.spi.v2.item.command.RegisterItemForCustomerCommand;
import com.winit.mms.spi.v2.mt.vo.ImportCheckErrorMessagesVO;
import com.winit.mms.spi.v2.mt.vo.ImportItemMtTaskVo;
import com.winit.mms.spi.vo.ItemForCustomerVO;
import com.winit.mms.spi.vo.ProductCheckErrorMessagesVO;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.QueryCustomerByCBpartnerIdCommand;
import com.winit.ums.spi.commnad.customer.agreement.QueryCustomerAgreementsByCustomerIdAndTypeCommand;
import com.winit.ums.spi.vo.CustomerInfoVO;
import com.winit.ums.spi.vo.customer.agreement.CustomerAgreementVO;

/**
 * mms商品服务适配
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * minghao.deng 	1.0  		2017年12月9日 	Created
 *
 * </pre>
 * @since 1.
 */
@Service("mms3ItemServiceAdapter")
public class MMS3ItemServiceAdapter {

    @Resource
    private ItemService        itemService;

    @Resource
    private UmsCustomerService umsCustomerService;

    /**
     * 校验用户是否签署协议
     */
    public void checkCustomerAgreement() {
        QueryCustomerAgreementsByCustomerIdAndTypeCommand command = new QueryCustomerAgreementsByCustomerIdAndTypeCommand();
        CommandContext context = CommandContext.getContext();

        command.setType("SECRECY");
        command.setUsername(context.getUsername());
        command.setCtx(context);
        CustomerAgreementVO customerAgreementVO = umsCustomerService.queryCustomerAgreementsByCustomerIdAndType(command);
        if (!"AGREE".equalsIgnoreCase(customerAgreementVO.getStatus())) {
            throw new ApiException(ErrorCode.MMS_AUTH_CHECK_FAIL);
        }
    }

    /**
     * 修改商品信息
     * 
     * @param command
     */
    public List<ProductCheckErrorMessagesVO> modifyItemForCustomer(ModifyProductForCustomerCommand command) {
        List<ProductCheckErrorMessagesVO> messagesVOs = new ArrayList<ProductCheckErrorMessagesVO>();
        CustomerInfoVO customerInfoVO = getCustomerInfo();
        if (customerInfoVO == null) {
            ProductCheckErrorMessagesVO messagesVO = new ProductCheckErrorMessagesVO();
            messagesVO.setErrorMessages("用户存在");
            messagesVO.setRow(0);
            messagesVOs.add(messagesVO);

            return messagesVOs;
        }

        ModifyItemForCustomerCommand modifyCommand = new ModifyItemForCustomerCommand();
        BeanUtils.copyProperties(command, modifyCommand, ModifyItemForCustomerCommand.class);
        modifyCommand.setCustomerCode(customerInfoVO.getCode());
        modifyCommand.setCustomerName(customerInfoVO.getName());

        List<ImportCheckErrorMessagesVO> checkErrorMessagesVOs = itemService.modifyItemForCustomer(modifyCommand);

        for (ImportCheckErrorMessagesVO checkErrorMessagesVO : checkErrorMessagesVOs) {
            ProductCheckErrorMessagesVO messagesVO = new ProductCheckErrorMessagesVO();
            BeanUtils.copyProperties(checkErrorMessagesVO, messagesVO);
            messagesVOs.add(messagesVO);
        }
        return messagesVOs;

    }

    /**
     * 注册商品
     * 
     * @param command
     */
    public List<ProductCheckErrorMessagesVO> registerItemForCustomer(ItemForCustomerCommand command) {
        List<ProductCheckErrorMessagesVO> messagesVOs = new ArrayList<ProductCheckErrorMessagesVO>();
        CustomerInfoVO customerInfoVO = getCustomerInfo();
        if (customerInfoVO == null) {
            ProductCheckErrorMessagesVO messagesVO = new ProductCheckErrorMessagesVO();
            messagesVO.setErrorMessages("用户不存在");
            messagesVO.setRow(0);
            messagesVOs.add(messagesVO);

            return messagesVOs;
        }

        RegisterItemForCustomerCommand registerCommand = new RegisterItemForCustomerCommand();
        registerCommand.setCustomerCode(customerInfoVO.getCode());
        registerCommand.setCustomerName(customerInfoVO.getName());

        List<ImportItemMtTaskVo> dataSet = new ArrayList<ImportItemMtTaskVo>();
        for (ItemForCustomerVO itemForCustomerVO : command.getProductList()) {
            ImportItemMtTaskVo taskVo = new ImportItemMtTaskVo();
            BeanUtils.copyProperties(itemForCustomerVO, taskVo, ImportItemMtTaskVo.class);
            dataSet.add(taskVo);
        }
        registerCommand.setDataSet(dataSet);
        // BUG:33826, 修改有错误信息返回不了的问题， 修改人：zhouxq , 日期：2017-7-8
        List<ImportCheckErrorMessagesVO> importCheckErrorMessagesVOs = itemService.registerItemForCustomer(registerCommand);
        for (ImportCheckErrorMessagesVO importCheckErrorMessagesVO : importCheckErrorMessagesVOs) {
            ProductCheckErrorMessagesVO messagesVO = new ProductCheckErrorMessagesVO();
            BeanUtils.copyProperties(importCheckErrorMessagesVO, messagesVO);
            messagesVOs.add(messagesVO);
        }
        return messagesVOs;
    }

    private CustomerInfoVO getCustomerInfo() {
        QueryCustomerByCBpartnerIdCommand idCommand = new QueryCustomerByCBpartnerIdCommand();
        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = ctx.get(ApiConstant.API_BPARTNER);
        idCommand.setCtx(ctx);
        idCommand.setcBpartnerId(bpartner.getcBpartnerId());
        return umsCustomerService.queryCustomerByCBpartnerId(idCommand);
    }

}
